<html><head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"><title>UserGuide - Some Tips &amp; Tricks</title></head>
<body bgcolor="#FFFFDF" link="#009999" vlink="#006666" alink="#006666">
<font face="Arial" size="2"><p align="center"><b><font size="5">UserGuide - Some Tips &amp; Tricks</font></b></p>

<p><b>"Using a map to index a list"</b></p><blockquote>


<a href="../reference/newlist.html">lists</a> provide a powerful way to build a structured 
storage system - however they have a disadvantage. If you aren't sure exactly where 
in the list a particular item is, you must examine each entry in the list to find the 
right one. 
<br>
<br>
<a href="../reference/newmap.html">Maps</a> also provide a similar function but are indexed by a 
key value instead - however they too have a disadvantage, they don't maintain the 
order elements are inserted into the list. 
<br>
<br>
However by using a combination of the two, you can neatly avoid both of these issues... 
<br>
<br>
This example loads a <a href="../reference/structures.html">structured</a> <a href="../reference/newlist.html">list</a> 
of book data and builds an index of ISBN numbers using a <a href="../reference/newmap.html">map</a>. 
It then demonstrates how to access the list using the index in the map. 

 

<pre><font face="Courier New, Courier, mono"size="2">  <b><font color="#006666">EnableExplicit</font></b>
  
  <font color="#006666">; A book catalog structure.</font>
  <b><font color="#006666">Structure</font></b> BOOK
    Title.s
    Author.s
    ISBN13.s
    Price.d
  <b><font color="#006666">EndStructure</font></b>
  
  <font color="#006666">; Create a list to hold the catalog entries.</font>
  <b><font color="#006666">NewList</font></b> <font color="#006666">Catalog</font>.BOOK()
  <font color="#006666">; Create a map to hold the ISBN index.</font>
  <b><font color="#006666">NewMap</font></b> <font color="#006666">ISBN13</font>.l()
  <font color="#006666">; Working variables.</font>
  <b><font color="#006666">Define</font></b>.l Count, Index
  <b><font color="#006666">Define</font></b>.s ISBN
  
  <font color="#006666">; Add an empty element at the top of the list.</font>
  <font color="#006666">; The first element in a list is numbered zero, however the map will return zero if a requested entry isn't present.</font>
  <font color="#006666">; This empty element avoids a potential problem with an incorrect reference to the first catalog item being returned.</font>
<font color="#006666">  AddElement</font>(<font color="#006666">Catalog</font>())
  
  <b><font color="#006666">For</font></b> Count = 1 <b><font color="#006666">To</font></b> 5
    
    <font color="#006666">; Read the data from the table into the list.</font>
<font color="#006666">    AddElement</font>(<font color="#006666">Catalog</font>())
    <b><font color="#006666">Read</font></b>.s <font color="#006666">Catalog</font>()\Title
    <b><font color="#006666">Read</font></b>.s <font color="#006666">Catalog</font>()\Author
    <b><font color="#006666">Read</font></b>.s <font color="#006666">Catalog</font>()\ISBN13
    <b><font color="#006666">Read</font></b>.d <font color="#006666">Catalog</font>()\Price
    
    <font color="#006666">; Index the ISBN to the map.</font>
<font color="#006666">    ISBN13</font>(<font color="#006666">Catalog</font>()\ISBN13) =<font color="#006666"> ListIndex</font>(<font color="#006666">Catalog</font>())
    
  <b><font color="#006666">Next</font></b> Count
  
  <font color="#006666">; Find an entry.</font>
  ISBN = "978-0340896983"
  Index =<font color="#006666"> ISBN13</font>(ISBN)
  
  <b><font color="#006666">If</font></b> Index &gt; 0
    <b><font color="#006666">Debug</font></b> "Book with ISBN13 " + ISBN + " is at list index " +<font color="#006666"> StrU</font>(Index) + "."
    <b><font color="#006666">Debug</font></b> ""
    
    <font color="#006666">; We can now directly select the right list element without having to perform a search.</font>
<font color="#006666">    SelectElement</font>(<font color="#006666">Catalog</font>(), Index)
    <b><font color="#006666">Debug</font></b> "'" +<font color="#006666"> Catalog</font>()\Title + "' by " +<font color="#006666"> Catalog</font>()\Author
    <b><font color="#006666">Debug</font></b> "ISBN: " +<font color="#006666"> Catalog</font>()\ISBN13
    <b><font color="#006666">Debug</font></b> "Price: " +<font color="#006666"> StrD</font>(<font color="#006666">Catalog</font>()\Price, 2)
    
  <b><font color="#006666">Else</font></b>
    <b><font color="#006666">Debug</font></b> "No book with that ISBN in the catalog."
    
  <b><font color="#006666">EndIf</font></b>
  
  <b><font color="#006666">End</font></b>
  
  <font color="#006666">; Some test data.</font>
  <b><font color="#006666">DataSection</font></b>
    
    BookData:
    
    <b><font color="#006666">Data</font></b>.s "Carte Blanche", "Jeffery Deaver", "978-1444716474"
    <b><font color="#006666">Data</font></b>.d 19.99
    
    <b><font color="#006666">Data</font></b>.s "One Day", "David Nicholls", "978-0340896983"
    <b><font color="#006666">Data</font></b>.d 7.99
    
    <b><font color="#006666">Data</font></b>.s "Madeleine", "Kate McCann", "978-0593067918"
    <b><font color="#006666">Data</font></b>.d 20.00
    
    <b><font color="#006666">Data</font></b>.s "The Dukan Diet", "Dr Pierre Dukan", "978-1444710335"
    <b><font color="#006666">Data</font></b>.d 8.99
    
    <b><font color="#006666">Data</font></b>.s "A Game of Thrones", "George R. R. Martin", "978-0006479888"
    <b><font color="#006666">Data</font></b>.d 9.99
    
    <b><font color="#006666">Data</font></b>.s "The Help", "Kathryn Stockett", "978-0141039282"
    <b><font color="#006666">Data</font></b>.d 8.99
    
  <b><font color="#006666">EndDataSection</font></b>
</font></pre>


</blockquote>
<p><b>UserGuide Navigation</b></p><blockquote>

<a href="../reference/ug_advanced.html">&lt; Previous: Advanced functions</a> | <a href="../reference/ug_overview.html">Overview</a> 
</body></html>